﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITJZ.BitHelper.Local.UI.IComponent;
using System.Xml;
using ITJZ.BitHelper.IAPI;

namespace ITJZ.BitHelper.Local.UI.Component
{
    public partial class ucSearch : UserControl, ISearchKnowledge
    {

        IKnowledge apiKnowledge = new ITJZ.BitHelper.Local.API.Knowledge();

        public ucSearch()
        {
            InitializeComponent();
        }

        #region ISearchKnowledge 成员

        public string Language
        {
            get;
            set;
        }

        public string Library
        {
            get;
            set;
        }

        public string KnowledgeGuid
        {
            get;
            set;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void Initialize()
        {
            FillMenuTree();  
        }

        /// <summary>
        /// 填充目录树
        /// </summary>
        void FillMenuTree()
        {
            XmlDocument doc = apiKnowledge.Menu();

            XmlNode root = doc.SelectSingleNode("menu");

            foreach (XmlNode cnode in root.ChildNodes)
            {

                string title = cnode.Attributes["title"].Value;
                string value = cnode.Attributes["value"].Value;

                TreeNode node = new TreeNode(title);
                node.Tag = value;

                treeView1.Nodes.Add(node);


                foreach (XmlNode c2 in cnode.ChildNodes)
                {
                    TreeNode t2 = new TreeNode(c2.Attributes["title"].Value);
                    t2.Tag = c2.Attributes["value"].Value;

                    node.Nodes.Add(t2);
                }
            }
        }

     
        public event EventHandler SelectChange;

        #endregion

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                this.Language = e.Node.Tag as string;
                this.Library = "(0)";
            }
            else
            {
                this.Language = e.Node.Parent.Tag as string;
                this.Library = e.Node.Tag as string;
            }

            //用被选中的库去填充第二个treeview
            FillFunList("");
        }

        private void treeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string guid = e.Node.Name;//获取所选知识的guid

            if (guid!=this.KnowledgeGuid )
            {
                this.KnowledgeGuid = guid;
                SelectChange(this, new EventArgs());//触发选择改变事件
            }
        }


        private void FillFunList(string keyword)
        {
            List<APIModel.Knowledge> list = apiKnowledge.Search(this.Language, this.Library, keyword);
            treeView2.Nodes.Clear();
            foreach (APIModel.Knowledge item in list)
            {
                treeView2.Nodes.Add(item.Guid, item.FullName);
            }

        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            FillFunList(textBox1.Text);
        }

        private void ucSearch_Load(object sender, EventArgs e)
        {
            try
            {

                XmlDocument doc = apiKnowledge.Menu();

                XmlNode root = doc.SelectSingleNode("menu");

                foreach (XmlNode cnode in root.ChildNodes)
                {

                    string title = cnode.Attributes["title"].Value;
                    string value = cnode.Attributes["value"].Value;

                    TreeNode node = new TreeNode(title);
                    node.Tag = value;

                    treeView1.Nodes.Add(node);


                    foreach (XmlNode c2 in cnode.ChildNodes)
                    {
                        TreeNode t2 = new TreeNode(c2.Attributes["title"].Value);
                        t2.Tag = c2.Attributes["value"].Value;

                        node.Nodes.Add(t2);
                    }
                }
            }
            catch { }
        }
    }
}
